package modelLayer;
import java.util.ArrayList;


/**
 * A container for the class DVD
 * 
 * @author Gruppe E
 * @version 1.0
 */
public class DVDContainer
{
    private ArrayList<DVD> dvds;
    private static DVDContainer instance;

    /**
     * Constructor for objects of class DVDContainer
     */
    private DVDContainer()
    {
        dvds = new ArrayList<DVD>();
    }

    /**
     * Method getInstance
     *
     * @return The return value
     */
    public static DVDContainer getInstance()
    {
        if(instance == null)
        {
            instance = new DVDContainer();
        }
        return instance;
    }
    
    /**
     * Method addDVD
     *
     * @param d A parameter
     */
    public void addDVD(DVD d)
    {
        dvds.add(d);
    }
    
    /**
     * Method findDVD
     * 
     * This method will search for a barcode, and return the corresponding DVD
     *
     * @param barcode
     * @return the correct DVD
     */
    public DVD findDVD(String barcode)
    {
        DVD returnDVD = null;
        boolean found = false;
        int size = dvds.size();
        for(int i = 0;  i < size && !found;i++)
        {
            if(dvds.get(i).getBarcode().equals(barcode))
            {
                returnDVD = dvds.get(i);
                found = true;
            }
        }
        return returnDVD;
    }
    
    /**
     * Method deleteDVD
     * 
     * deletes a DVD that matches the given barcode
     *
     * @param barcode A parameter
     * @return The return value
     */
    public boolean deleteDVD(String barcode)
    {
        boolean successful = true;
        if(findDVD(barcode)==null)
        {
            successful = false;
        }
        DVD tempD = findDVD(barcode);
        dvds.remove(tempD);

        return successful;
    }
    
    /**
     * Method updateDVD
     * 
     * searches for the DVDs barcode, and updates the DVDs information
     *
     * @param oldBarcode A parameter
     * @param title A parameter
     * @param artist A parameter
     * @param publicationDate A parameter
     * @param newBarcode A parameter
     * @return The return value
     */
    public boolean updateDVD(String oldBarcode, String title, String artist, String publicationDate, String newBarcode)
    {
        boolean successful = false;
        DVD tempD = findDVD(oldBarcode);
        tempD.setArtist(artist);
        tempD.setBarcode(newBarcode);
        tempD.setPublicationDate(publicationDate);
        tempD.setTitle(title);
        if(tempD.getArtist().equals(artist) && tempD.getBarcode().equals(newBarcode) && tempD.getPublicationDate().equals(publicationDate) && tempD.getTitle().equals(title))
        {
            successful = true;
        }
        return successful;
    }
}
